///**
// * MongoClientFactory.java
// */
//package com.mht2017.common.mongo;
//
//import com.mht2017.common.utils.IpPortSpliter;
//import com.mht2017.common.utils.SizeUtil;
//import com.mongodb.*;
//import org.apache.commons.collections.CollectionUtils;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
//import java.net.UnknownHostException;
//import java.util.Arrays;
//import java.util.LinkedList;
//import java.util.List;
//
//
//public class MongoClientFactory {
//
//	private static final Logger LOGGER = LoggerFactory.getLogger(MongoClientFactory.class);
//	private static final String actionName = "Mongo客户端工厂";
//
//	private static MongoClient mongoClient;
//
//	public static DB connectToDB(String connectString, String database, String userName, String password) {
//		if (null == connectString || null == database || null == userName || null == password) {
//			throw new MongoException("参数不应该为null");
//		}
//
//		final int DEFAULT_PORT = 27017;
//		List<ServerAddress> servers = new LinkedList<>();
//		List<IpPortSpliter.IpPort> ipPorts = IpPortSpliter.split(connectString, DEFAULT_PORT);
//		if (CollectionUtils.isNotEmpty(ipPorts)) {
//			for (IpPortSpliter.IpPort ipPort : ipPorts) {
//				try {
//					servers.add(new ServerAddress(ipPort.getIp(), ipPort.getPort()));
//				} catch (UnknownHostException e) {
//					LOGGER.error(String.format("%s-初始化MongoDB的连接信息(%s)出错: '%s'", actionName, ipPort, e.getMessage()), e);
//				}
//			}
//		} else {
//			throw new MongoException("MongoDB的连接信息未配置");
//		}
//
//		try {
//			List<MongoCredential> mongoCredentials = Arrays.asList(MongoCredential.createMongoCRCredential(userName, database, password.toCharArray()));
//			if (SizeUtil.size(servers) == 1) {
//				mongoClient = new MongoClient(servers.iterator().next(), mongoCredentials);
//			} else if (SizeUtil.size(servers) > 1) {
//				mongoClient = new MongoClient(servers, mongoCredentials);
//			} else {
//				throw new MongoException("MongoDB的连接信息未配置");
//			}
//			return mongoClient.getDB(database);
//		} catch (Exception e) {
//			throw new MongoException("初始化MongoDB服务的时候出错", e);
//		}
//	}
//
//	public static void close() {
//		if (null != mongoClient) {
//			mongoClient.close();
//		}
//	}
//
//}
